<?php
namespace Siter\Controller;

class ZoneStatsController extends Controller {

    public function index() {
        $m              = D2('ZoneStats');
        $day            = I('get.day', null);
        $field          = 'day, zone_id, SUM(num) AS num, SUM(siter_income) AS siter_income';
        $this->sortExpr = 'num DESC';
        $record = $m->field($field)->where($this->indexExpr)->group('day, zone_id')->order('day DESC, num DESC')->select();
        $total  = array('zone_num' => 0, 'num' => 0, 'siter_income' => 0);

        if(!empty($record)) {
            foreach($record as $v) {
                $total['zone_num']     += 1;
                $total['num']          += $v['num'];
                $total['siter_income']  = bcadd($total['siter_income'], $v['siter_income'], C('MONEY_SCALE'));
            }
        }

        $this->assign('total', $total);
        $this->assign('record', $record)->display();
    }

    public function index2() {
        $zoneId = I('get.zone_id', 0, 'intval');
        $day    = I('get.day', '');

        if(empty($zoneId) || empty($day)) {
            throw new ThinkException('参数不完整');
        }

        $expr = array(
            'zone_id' => $zoneId,
            'day'     => $day,
        ); 
        $sortExpr = 'num DESC';
        $m        = D2('ZoneStats');
        $field    = 'plan_id, SUM(num) AS num, SUM(siter_income) AS siter_income';
        $page     = $m->field($field)->where($expr)->group('plan_id')->order($sortExpr)->page2();
        $total    = array('num' => 0, 'siter_income' => 0);

        if(!empty($page['record'])) {
            foreach($page['record'] as $v) {
                $total['num']          += $v['num'];
                $total['siter_income']  = bcadd($total['siter_income'], $v['siter_income'], C('MONEY_SCALE'));
            }
        }

        $this->assign('total', $total);
        $this->assign('page', $page)->display();
    }

}
